/*
 * 时间相关工具函数
 */
var now = new Date(),
      month = now.getMonth(),
      year = now.getFullYear(),
      date = now.getDate();      
export default {
    toStr: function(num){
      return num >= 10 ? num : '0' + num
    },
    // 上个月，格式 2018-04
    getLastMonth: function(){
      var newDate = new Date(now.getFullYear() , now.getMonth() -1),
          lastMonth = this.toStr(newDate.getMonth()+1)
      return newDate.getFullYear() + '-' + lastMonth;
    },
    // 当前月，格式 2018-05
    getCurMonth: function(){
      var curMonth = this.toStr(month + 1) 
      return  year + '-' + curMonth
    },
    /*
    * 传入时间戳格式，返回格式化时间
    * eg: moment.formatDate(new Date() , 'month') => 2018-05
    */
    formatDate: function(date,type){
      var y = date.getFullYear();
      if(type && type == 'year') return y
      var m = date.getMonth() + 1;
      m = m < 10 ? '0' + m : m;
      if(type && type == 'month') return y + '-' + m
      var d = date.getDate();
      d = d < 10 ? ('0' + d) : d;
      return y + '-' + m + '-' + d;
    }
}